package org.jeecg.modules.golf.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.golf.question.client.qto.QuestionQueryPageDTO;
import org.jeecg.modules.golf.user.client.*;
import org.jeecg.modules.golf.user.entity.Trainee;
import org.jeecg.modules.golf.user.mapper.TraineeMapper;
import org.jeecg.modules.golf.user.service.ITraineeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 学员
 * @Author: jeecg-boot
 * @Date:   2020-11-01
 * @Version: V1.0
 */
@Service
public class TraineeServiceImpl extends ServiceImpl<TraineeMapper, Trainee> implements ITraineeService {


    private int statisticsStep = 5;


    @Override
    public Page<TraineeRealNameDistDTO> distName(QuestionQueryPageDTO questionQueryPageDTO){

        return getBaseMapper().distName(questionQueryPageDTO);

    }

    @Override
    public TraineeSexDistDTO distSex(QuestionQueryPageDTO questionQueryPageDTO){
        return getBaseMapper().distSex(questionQueryPageDTO);
    }





    /**
     * 年龄分布
     *
     * @param questionQueryPageDTO
     * @return
     */
    @Override
    public List<TraineeAgeDistDTO> distAge(QuestionQueryPageDTO questionQueryPageDTO) {
        int with = 20;
        List<TraineeAgeDistDTO> traineeAgeDistDTOList = new ArrayList<>(with);
        for(int i =0 ;i<with;i++){
            TraineeAgeDistDTO traineeAgeDistDTO = new TraineeAgeDistDTO();
            traineeAgeDistDTO.setBracketStart((statisticsStep * i)+1);
            traineeAgeDistDTO.setBracketEnd(statisticsStep * (i+1));
            traineeAgeDistDTO.setPersons(0);
            traineeAgeDistDTOList.add(traineeAgeDistDTO);
        }
        List<TraineeAgeDistDTO> sourceAgeDistDTOList  = getBaseMapper().distAge(questionQueryPageDTO);
        sourceAgeDistDTOList.forEach(i->{
            setDist(i,traineeAgeDistDTOList);
        });
        return traineeAgeDistDTOList.stream().filter(i->i.getPersons()>0).collect(Collectors.toList());

    }

    /**
     * 差点分布
     * @param questionQueryPageDTO
     * @return
     */
    @Override
    public List<TraineeChadianDistDTO> distChadian(QuestionQueryPageDTO questionQueryPageDTO) {
        int with = 20;
        List<TraineeChadianDistDTO> traineeChadianDistDTOList = new ArrayList<>(with);
        for(int i =0 ;i<with;i++){
            TraineeChadianDistDTO chadianDistDTO = new TraineeChadianDistDTO();
            chadianDistDTO.setBracketStart((statisticsStep * i)+1);
            chadianDistDTO.setBracketEnd(statisticsStep * (i+1));
            chadianDistDTO.setPersons(0);
            traineeChadianDistDTOList.add(chadianDistDTO);
        }
        List<TraineeChadianDistDTO> source = getBaseMapper().distChadian(questionQueryPageDTO);
        source.forEach(i->{
            setDist(i,traineeChadianDistDTOList);
        });
        return traineeChadianDistDTOList.stream().filter(i->i.getPersons()>0).collect(Collectors.toList());


    }

    public <T extends Bracket> void setDist(Bracket bracket, List<T> bracketList ){
        for(Bracket i : bracketList){
            if(bracket.getValue() !=null && bracket.getValue()>= i.getBracketStart()&& bracket.getValue() < i.getBracketEnd()){
                i.setPersons(i.getPersons()+bracket.getPersons());
                break;
            }
        }
    }

}
